package com.thecafetechno.main;

import java.io.Serializable;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.thecafetechno.university.Locker;
import com.thecafetechno.university.Student;
import com.thecafetechno.util.HibernateUtil;

public class OneToOneTest {

	public static void main(String[] args) {
		
		Locker locker = new Locker();
		locker.setLocation("320, Building 1, First Floor");
				
		Student student = new Student();
		student.setName("Saurabh");
		student.setLocker(locker);
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction transaction = session.beginTransaction();
		// id returned is saved.
		Serializable id = session.save(student); 
		transaction.commit();
		
		System.out.println("\nAfter Saving");
		// The following fragment retrieves the object just saved
		// we first reopen session	
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = session.beginTransaction();
		Student studentAfterRetrieval = (Student)(session.load(Student.class, id));
		transaction.commit();
		
		System.out.println("Locker Location is : " +studentAfterRetrieval.getLocker().getLocation());
	}

}
